package SecondeStage.FanShe;

import java.lang.reflect.Method;

public class ReflectAccessMethod {
    public static void main(String[] args) throws Exception{
        // 演示通过反射调用方法
        //1. 得到Boss类对应的Class对象
        Class<?> bossCls = Class.forName("SecondeStage.FanShe.Boss");
        //2. 创建对象
        Object o = bossCls.newInstance();
        //3. 调用public的hi方法
        Method hi = bossCls.getMethod("hi",String.class);
        hi.invoke(o,"hsp");
        //4. 调用private static 方法
        Method say=bossCls.getDeclaredMethod("say", int.class, String.class, char.class);
        //say.invoke(o,100,"张三",'男'); 报错，say方法是私有的，需要暴破
        say.setAccessible(true);
        System.out.println(say.invoke(o,100,"张三",'男'));
        // 因为say方法是static的，还可以这样
        System.out.println(say.invoke(null,100,"张三",'男'));
        //5. 在反射中，如果方法有返回值，统一返回Object
        Object reVal = say.invoke(null,300,"李四",'男');
        System.out.println("reVal 的运行类型="+reVal.getClass()); //String
    }
}

class Boss{
    public int age;
    private static String name;

    public Boss() {
    }

    private  static String say(int n, String s, char c){
        return n+" "+s+" "+c;
    }

    public void hi(String name){
        System.out.println("hi!"+name);
    }
}